﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MegaBitProject.Models;
using MegaBitProject.ViewModels;

namespace MegaBitProject.Controllers
{
    public class HomeController : Controller
    {
        private IGameRepository rep = null;

        public HomeController()
        {
            rep = new GameRepository();
        }

        public HomeController(IGameRepository _rep)
        {
            rep = _rep;
        }

        public ActionResult Index()
        {
            var model = (from g in rep.getGames
                         select g).Take(2);
            return View(model);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult _Highscore()
        {
            HighScore view = new HighScore();
            var highest = (from user in rep.getScore
                           orderby user.score descending
                           select user).Take(10);

            view.score = new List<int>();
            view.name = new List<string>();

            foreach (var h in highest)
            {
                view.score.Add(h.score);

                var name = (from user in rep.getUser
                            where user == h.userID
                            select user).SingleOrDefault();

                view.name.Add(name.UserName);
            }

            return PartialView(view);
        }
        public ActionResult _CurrentGames()
        {
            CurrentGames view = new CurrentGames();

            view.id = new List<int>();
            view.maxPlayers = new List<int>();
            view.name = new List<string>();
            view.numPlayers = new List<int>();
            view.controller = new List<string>();

            var games = (from game in rep.getOpenGames
                         orderby game.ID descending
                         select game).ToList();

            foreach (var g in games)
            {
                var numP = (from game in rep.getPlayedGames
                            where game.gameID == g
                            select game).Count();

                var maxP = (from game in rep.getOpenGames
                            where game == g
                            select game).SingleOrDefault();

                if (numP < maxP.maxPlayers)
                {
                    view.numPlayers.Add(numP);
                    view.maxPlayers.Add(maxP.maxPlayers);
                    var name = (from game in rep.getGames
                                where game == maxP.appID
                                select game).SingleOrDefault();

                    view.name.Add(name.name);
                    view.id.Add(maxP.ID);
                    view.controller.Add(name.controller);
                }
            }

            return PartialView(view);
        }
    }
}